package com.gulimall.member.controller;

import com.gulimall.common.exception.BizCodeEnum;
import com.gulimall.common.to.AuthSocialUserTo;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.R;
import com.gulimall.common.vo.MemberRespVo;
import com.gulimall.member.entity.MemberEntity;
import com.gulimall.member.exception.PhoneExistsException;
import com.gulimall.member.exception.UsernameExistsException;
import com.gulimall.member.feign.CouponFeignService;
import com.gulimall.member.service.MemberService;
import com.gulimall.member.vo.MemberLoginVo;
import com.gulimall.member.vo.MemberRegisterVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 会员
 *
 * @author kkk
 * @email 2017809755@qq.com
 * @date 2022-05-17 21:59:00
 */
@RestController
@Slf4j
@RequestMapping("member/member")
@RefreshScope
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;

    @Value("${memberUser.name}")
    private String name;
    @Value("${memberUser.age}")
    private Integer age;

    /**
     * 测试nacos 配置中心
     */
    @GetMapping("/test")
    public R getProperties() {
        return R.ok().put("name", name).put("age", age);
    }

    /**
     * 测试openFeign远程调用
     *
     * @return
     */
    @GetMapping("/coupon/list")
    public R getCouponList() {
        return couponFeignService.memberCoupons();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    @PostMapping("/register")
    public R registerMember(@RequestBody MemberRegisterVo registerVo) {
        Map<String, Object> errorMap = new HashMap<>();
        try {
            memberService.registerMember(registerVo);
        } catch (UsernameExistsException e) {
            log.error(e.toString());
            errorMap.put("username", e.getMessage());
            return R.error().put("errorMap", errorMap);
        } catch (PhoneExistsException e) {
            log.error(e.toString());
            errorMap.put("phone", e.getMessage());
            return R.error().put("errorMap", errorMap);
        }

        return R.ok();
    }

    @PostMapping("/login")
    public R memberLogin(@RequestBody MemberLoginVo loginVo) {
        MemberEntity isLogin = memberService.loginMember(loginVo);
        if (isLogin != null) {
            MemberRespVo respVo = new MemberRespVo();
            BeanUtils.copyProperties(isLogin, respVo);
            return R.ok().put("data", respVo);
        } else {
            return R.error(BizCodeEnum.LOGIN_PASSWORD_INVAILD_EXCEPTION.getCode(),
                    BizCodeEnum.LOGIN_PASSWORD_INVAILD_EXCEPTION.getMsg());
        }
    }

    @PostMapping("/oauth/login")
    public R memberOAuthLogin(@RequestBody AuthSocialUserTo socialUserTo) {
        MemberEntity member = memberService.oAuthLogin(socialUserTo);
        if (member != null) {
            MemberRespVo respVo = new MemberRespVo();
            BeanUtils.copyProperties(member, respVo);
            return R.ok().put("data", respVo);
        } else {
            return R.error(BizCodeEnum.LOGIN_PASSWORD_INVAILD_EXCEPTION.getCode(),
                    BizCodeEnum.LOGIN_PASSWORD_INVAILD_EXCEPTION.getMsg());
        }
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
